<?php
namespace app\admin\model;

use app\index\model\Cate;
use think\Model;

class Item extends Model{

    private $itemModel;
    public function __construct(){
        parent::__construct();
        $this -> itemModel = db('item');
    }
    /*  新增项目
     *  @param  Array   [data]
     *  @return Array   [rinfo]
     * **/
    public function add($data){
        $result = $this -> itemModel -> insertGetId($data);
        return $result;
    }
    /*  修改项目
     *  @param  Array   [where,save]
     *  @return Array   [rinfo]
     * **/
    public function modify($where,$save){
        $result = $this -> itemModel -> where($where) -> update($save);
        return $result;
    }
    /*  查看列表
     *  @param  Array   [data]
     *  @return Array   [rinfo]
     * **/
    public function getItemDataList($where, $order='id desc', $num=10,&$page,$type=1){
        $itemData = $this -> itemModel -> where($where) -> order($order) -> paginate($num,false,['query'=>request()->param() ]);
        $page = $itemData->render();
        $data = iterator_to_array($itemData);
        if(empty($data)){   return [];  }
        switch ($type){
            case 1:
                $dataList = $this -> formatItemDatForList($data);
                break;
        }
        return $dataList;

    }
    /*  格式化数据
     *  @param  Array   [data]
     *  @return Array   [rinfo]
     * **/
    public function formatItemDatForList($data){
        $indexCateModel = new Cate();
        foreach ($data as $key => $value) {
            $list[$key]['id'] = $value['id'];
            $list[$key]['url'] = $value['url'];
            $list[$key]['title'] = $value['title'];
            $list[$key]['reco_index'] = $value['reco_index'];
            $list[$key]['order'] = $value['order'];
            $list[$key]['read_num'] = $value['read_num'];
            $list[$key]['pname'] = $indexCateModel -> getCateValue(['id'=> $value['cate_pid']],'name');
            $list[$key]['cname'] = $indexCateModel -> getCateValue(['id'=> $value['cate_id']],'name');
            $list[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']);
        }
        return $list;
    }


}